package com.baby.service;

import java.sql.SQLException;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baby.util.DataBasePool;
import com.mchange.v2.c3p0.PooledDataSource;

@Service
public class MonitorService {

	@Autowired
	DataSource dataSource;

	public DataBasePool getDataBasePool() throws SQLException {

		DataBasePool dataBasePool = new DataBasePool();

		if (dataSource instanceof PooledDataSource) {
			PooledDataSource pds = (PooledDataSource) dataSource;

			dataBasePool.setNumBusyConnections(pds.getNumBusyConnectionsAllUsers());
			dataBasePool.setNumConnections(pds.getNumConnectionsAllUsers());
			dataBasePool.setNumIdleConnections(pds.getNumIdleConnectionsAllUsers());
			dataBasePool.setNumUserPools(pds.getNumUserPools());

		} else
			System.err.println("Not a c3p0 PooledDataSource!");
		return dataBasePool;
	}

}
